package com.cskaoyan.wordmemorize.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.wordmemorize.common.aspect.operate.OperateLog;
import com.cskaoyan.wordmemorize.common.constant.ResultCodeEnum;
import com.cskaoyan.wordmemorize.common.exception.BusinessException;
import com.cskaoyan.wordmemorize.converter.LevelConverter;
import com.cskaoyan.wordmemorize.dao.entity.LevelDO;
import com.cskaoyan.wordmemorize.dao.mapper.LevelMapper;
import com.cskaoyan.wordmemorize.dao.mapper.LevelPrivilegeMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteLevelOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.LevelDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.LevelCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.service.LevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class LevelServiceImpl implements LevelService {

    @Autowired
    LevelMapper levelMapper;

    @Autowired
    LevelConverter levelConverter;

    @Autowired
    LevelPrivilegeMapper levelPrivilegeMapper;

    /**
     * 新增等级
     * @param command
     */
    @Override
    @OperateLog(dataType = "等级", operateType = 1, description = "新增等级")
    public void addLevel(LevelCommand command) {
        // 非空校验
        if (command.getLevelName() == null) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }
        if (command.getLevelValue() == 0) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }
        if (command.getCheckInDays() == 0) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }
        if (command.getCheckInType() != 1 && command.getCheckInType() != 2) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }
        if (command.getUnlockPrice() == null) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }
        // 判断等级是否存在
        LambdaQueryWrapper<LevelDO> addLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addLambdaQueryWrapper.eq(LevelDO::getLevelName,command.getLevelName());

        LevelDO levelDO = levelMapper.selectOne(addLambdaQueryWrapper);
        if (levelDO != null) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }

        // 保存等级
        LevelDO levelDO1 = levelConverter.levelCommandToLevelDO(command);
        levelMapper.insert(levelDO1);
    }

    /**
     * 更新等级
     * @param command
     */
    @Override
    @OperateLog(dataType = "等级", operateType = 4, description = "修改等级")
    public void updateLevel(LevelCommand command) {
        if (command.getId() == null) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }

        LevelDO levelDO = levelMapper.selectById(command.getId());
        if (levelDO == null) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }

        // 更新字段
        levelDO.setLevelName(command.getLevelName());
        levelDO.setLevelValue(command.getLevelValue());
        levelDO.setCheckInDays(command.getCheckInDays());
        levelDO.setCheckInType(command.getCheckInType());
        levelDO.setUnlockPrice(command.getUnlockPrice());
        levelDO.setDescription(command.getDescription());

        levelMapper.updateById(levelDO);
    }

    /**
     * 分页查询等级列表
     * @param request
     * @return
     */
    @Override
    public PageDTO<LevelDTO> getLevelList(PageRequest request) {
        if (request.getPageNum() == null) {
            // 查询所有的等级
            List<LevelDO> levelDOs = levelMapper.selectList(null);

            return levelConverter.levelDOToPageDTO(levelDOs,levelDOs.size());
        }

        // 需要分页
        Page<LevelDO> pageParam = new Page<>(request.getPageNum(), request.getCount());

        LambdaQueryWrapper<LevelDO> levelWrapper = new LambdaQueryWrapper<>();
        if (request.getKeyword() != null) {
            levelWrapper.like(LevelDO::getLevelName, request.getKeyword());
                    /*.or()
                    .like(LevelDO::getDescription, request.getKeyword());*/
        }
        Page<LevelDO> levelPage = levelMapper.selectPage(pageParam, levelWrapper);

        return levelConverter.levelDOToPageDTO(levelPage.getRecords(), (int) levelPage.getTotal());
    }

    /**
     * 删除等级
     * @param id
     * @return
     */
    @Override
    @OperateLog(dataType = "等级", operateType = 2, description = "删除等级", logReturnValue = true)
    public DeleteLevelOperateDTO deleteLevel(Long id) {
        // 查询
        LevelDO levelDO = levelMapper.selectById(id);
        if (levelDO == null) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }

        // 检查是否有用户关联此等级?

        //删除等级
        levelMapper.deleteById(id);
        return new DeleteLevelOperateDTO();
    }

    /**
     * 批量删除等级
     * @param command
     * @return
     */
    @Override
    @OperateLog(dataType = "等级", operateType = 3, description = "批量删除等级", logReturnValue = true)
    public List<DeleteLevelOperateDTO> deleteLevels(BatchDeleteCommand command) {
        return command.getIds().stream()
                .map(id -> {
                    try {
                        deleteLevel(Long.valueOf(id));
                        return new DeleteLevelOperateDTO();
                    } catch (Exception e) {
                        return new DeleteLevelOperateDTO();
                    }
                })
                .collect(Collectors.toList());
    }
}
